Adding Icons with Pseudo-Elements in CSS
Pseudo-elements like ::before and ::after can be used to insert icons or decorative symbols without adding extra HTML elements. This is especially useful when using icon fonts or Unicode characters.
Use content in combination with Unicode characters or icon fonts to display icons.
Pseudo-elements do not exist in the DOM, so they are purely visual and keep HTML clean.
You can style these icons with CSS properties like color, font-size, margin, and position.
Perfect for adding decorative symbols, social media icons, or arrows without extra markup.
In this example, the ::before pseudo-element inserts a gold star before the paragraph text. No additional HTML elements are needed to display the icon.
Here, the ::after pseudo-element adds a Font Awesome user icon to the button without modifying the HTML. You can control its position, size, and color entirely with CSS.
Use pseudo-elements for decorative or non-interactive icons to reduce HTML clutter.
Ensure accessibility by not relying solely on pseudo-elements for conveying critical information.
Combine with position, margin, and font-size to place icons precisely.
Test across browsers to ensure correct rendering of icon fonts or Unicode symbols.
How would you add a star icon after a rating number using only CSS, without adding any extra HTML elements?
What happens if you forget to set content: '' on a pseudo-element when trying to display an icon?
A team member added icons using pseudo-elements, but now screen readers are announcing the unicode characters — how would you debug and fix this?
We’re using icon fonts via pseudo-elements, but the icons disappear on mobile Safari — what could be causing this and how would you investigate?
You're designing a reusable button component that supports icons via pseudo-elements — how do you handle dynamic icon content, accessibility, and performance across 50+ variants?
In a large design system, pseudo-elements are used for icons everywhere — what are the scalability risks, and how would you mitigate them without forcing a full rewrite?
Our legacy UI uses pseudo-elements for icons across 200+ components, but we’re migrating to a new design system with SVG sprites — how would you plan and execute this transition without breaking accessibility or causing layout shifts?
You’re advising a company that uses pseudo-element icons for performance reasons, but now they’re getting complaints about inconsistent rendering across browsers — how do you balance performance, maintainability, and cross-platform reliability at the architecture level?